package com.strive.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * @description:
 * @author: bingcun.chen
 * @Date: 2022/2/24 16:48
 * @Version 1.0
 */
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    @Lazy
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

//    public PersistentTokenRepository persistentTokenRepository(){
//        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
//        //设置数据源
//        jdbcTokenRepository.setDataSource(dataSource);
//        return jdbcTokenRepository;
//    }
//
//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers("/oauth/check_token");
//
//    }

    //    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        String password = passwordEncoder().encode("123456");
//        auth.inMemoryAuthentication().passwordEncoder(passwordEncoder())
//                .withUser("fox123").password(password).roles("admin")
//                .and()
//                .withUser("fox456").password(password).roles("user");
//
//    }

    //自定义provider来处理用户名密码错误的问题
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        AuthenticationProvider authenticationProvider = new AuthenticationProvider();
//        authenticationProvider.setCustomerPasswordEncoder(passwordEncoder());
//        authenticationProvider.setUserService(userService);
//        auth.authenticationProvider(authenticationProvider);
//
//    }

//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.formLogin().permitAll()
////                .loginPage("/login.html")
////                .loginProcessingUrl("/user/login")
////                .defaultSuccessUrl("/main")
////                .failureForwardUrl("/err")
//                .and().authorizeRequests()
//                .antMatchers(ENDPOINTS).permitAll()
//                .anyRequest().authenticated()
//                .and().csrf().disable();
//
//        //记住我
////        http.rememberMe().tokenRepository(persistentTokenRepository())
////                .tokenValiditySeconds(3600)
////                .userDetailsService(userService);
//    }

    public static String[] getUrlList(){
        return ENDPOINTS;
    }


    private static final String[] ENDPOINTS = {
            "/oauth/**",
            "/actuator/**",
            "/*/v2/api-docs",
            "/swagger/api-docs",
            "/swagger-ui.html",
            "/doc.html",
            "/swagger-resources/**",
            "/webjars/**",
            "/druid/**",
            "/v2/api-docs",
            //customer
            "/user/login",
            "/login.html",
            "err",
            "/err.html",
            "/createUser",
//            "/testUser",
    };




}
